<!-- HTML header for doxygen 1.8.6-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<title>OpenCV: Point Polygon Test</title>
<link href="../../opencv.ico" rel="shortcut icon" type="image/x-icon" />
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<script type="text/javascript" src="../../tutorial-utils.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
//<![CDATA[
MathJax.Hub.Config(
{
  TeX: {
      Macros: {
          matTT: [ "\\[ \\left|\\begin{array}{ccc} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{array}\\right| \\]", 9],
          fork: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ \\end{array} \\right.", 4],
          forkthree: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ \\end{array} \\right.", 6],
          forkfour: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ #7 & \\mbox{#8}\\\\ \\end{array} \\right.", 8],
          vecthree: ["\\begin{bmatrix} #1\\\\ #2\\\\ #3 \\end{bmatrix}", 3],
          vecthreethree: ["\\begin{bmatrix} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{bmatrix}", 9],
          cameramatrix: ["#1 = \\begin{bmatrix} f_x & 0 & c_x\\\\ 0 & f_y & c_y\\\\ 0 & 0 & 1 \\end{bmatrix}", 1],
          distcoeffs: ["(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \\tau_x, \\tau_y]]]]) \\text{ of 4, 5, 8, 12 or 14 elements}"],
          distcoeffsfisheye: ["(k_1, k_2, k_3, k_4)"],
          hdotsfor: ["\\dots", 1],
          mathbbm: ["\\mathbb{#1}", 1],
          bordermatrix: ["\\matrix{#1}", 1]
      }
  }
}
);
//]]>
</script><script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
<link href="../../stylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<!--#include virtual="/google-search.html"-->
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="../../opencv-logo-small.png"/></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">OpenCV
   &#160;<span id="projectnumber">4.5.2</span>
   </div>
   <div id="projectbrief">Open Source Computer Vision</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "../../search",false,'Search');
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('../../',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="../../d9/df8/tutorial_root.html">OpenCV Tutorials</a></li><li class="navelem"><a class="el" href="../../d7/da8/tutorial_table_of_content_imgproc.html">Image Processing (imgproc module)</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">Point Polygon Test </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p><b>Prev Tutorial:</b> <a class="el" href="../../d0/d49/tutorial_moments.html">Image Moments</a></p>
<p><b>Next Tutorial:</b> <a class="el" href="../../d2/dbd/tutorial_distance_transform.html">Image Segmentation with Distance Transform and Watershed Algorithm</a></p>
<table class="doxtable">
<tr>
<th align="right"></th><th align="left"></th></tr>
<tr>
<td align="right">Original author </td><td align="left">Ana Huamán </td></tr>
<tr>
<td align="right">Compatibility </td><td align="left">OpenCV &gt;= 3.0 </td></tr>
</table>
<h2>Goal </h2>
<p>In this tutorial you will learn how to:</p>
<ul>
<li>Use the OpenCV function <a class="el" href="../../d3/dc0/group__imgproc__shape.html#ga1a539e8db2135af2566103705d7a5722">cv::pointPolygonTest</a></li>
</ul>
<h2>Theory </h2>
<h2>Code </h2>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'><p> This tutorial code's is shown lines below. You can also download it from <a href="https://github.com/opencv/opencv/tree/master/samples/cpp/tutorial_code/ShapeDescriptors/pointPolygonTest_demo.cpp">here</a> </p><div class="fragment"><div class="line"></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="../../d4/dd5/highgui_8hpp.html">opencv2/highgui.hpp</a>&quot;</span></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="../../d1/d4f/imgproc_2include_2opencv2_2imgproc_8hpp.html">opencv2/imgproc.hpp</a>&quot;</span></div><div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div><div class="line"></div><div class="line"><span class="keyword">using namespace </span><a class="code" href="../../d2/d75/namespacecv.html">cv</a>;</div><div class="line"><span class="keyword">using namespace </span>std;</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main( <span class="keywordtype">void</span> )</div><div class="line">{</div><div class="line">    <span class="keyword">const</span> <span class="keywordtype">int</span> r = 100;</div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> src = <a class="code" href="../../d3/d63/classcv_1_1Mat.html#a0b57b6a326c8876d944d188a46e0f556">Mat::zeros</a>( <a class="code" href="../../dc/d84/group__core__basic.html#ga346f563897249351a34549137c8532a0">Size</a>( 4*r, 4*r ), <a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga32b18d904ee2b1731a9416a8eef67d06">CV_8U</a> );</div><div class="line"></div><div class="line">    vector&lt;Point2f&gt; vert(6);</div><div class="line">    vert[0] = <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a>( 3*r/2, static_cast&lt;int&gt;(1.34*r) );</div><div class="line">    vert[1] = <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a>( 1*r, 2*r );</div><div class="line">    vert[2] = <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a>( 3*r/2, static_cast&lt;int&gt;(2.866*r) );</div><div class="line">    vert[3] = <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a>( 5*r/2, static_cast&lt;int&gt;(2.866*r) );</div><div class="line">    vert[4] = <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a>( 3*r, 2*r );</div><div class="line">    vert[5] = <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a>( 5*r/2, static_cast&lt;int&gt;(1.34*r) );</div><div class="line"></div><div class="line">    <span class="keywordflow">for</span>( <span class="keywordtype">int</span> i = 0; i &lt; 6; i++ )</div><div class="line">    {</div><div class="line">        <a class="code" href="../../d6/d6e/group__imgproc__draw.html#ga7078a9fae8c7e7d13d24dac2520ae4a2">line</a>( src, vert[i],  vert[(i+1)%6], <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>( 255 ), 3 );</div><div class="line">    }</div><div class="line"></div><div class="line">    vector&lt;vector&lt;Point&gt; &gt; contours;</div><div class="line">    <a class="code" href="../../d3/dc0/group__imgproc__shape.html#gadf1ad6a0b82947fa1fe3c3d497f260e0">findContours</a>( src, contours, <a class="code" href="../../d3/dc0/group__imgproc__shape.html#gga819779b9857cc2f8601e6526a3a5bc71ab10df56aed56c89a026580adc9431f58">RETR_TREE</a>, <a class="code" href="../../d3/dc0/group__imgproc__shape.html#gga4303f45752694956374734a03c54d5ffa5f2883048e654999209f88ba04c302f5">CHAIN_APPROX_SIMPLE</a>);</div><div class="line"></div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> raw_dist( src.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#a146f8e8dda07d1365a575ab83d9828d1">size</a>(), <a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga4a3def5d72b74bed31f5f8ab7676099c">CV_32F</a> );</div><div class="line">    <span class="keywordflow">for</span>( <span class="keywordtype">int</span> i = 0; i &lt; src.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abed816466c45234254d25bc59c31245e">rows</a>; i++ )</div><div class="line">    {</div><div class="line">        <span class="keywordflow">for</span>( <span class="keywordtype">int</span> j = 0; j &lt; src.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa3e5a47585c9ef6a0842556739155e3e">cols</a>; j++ )</div><div class="line">        {</div><div class="line">            raw_dist.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa5d20fc86d41d59e4d71ae93daee9726">at</a>&lt;<span class="keywordtype">float</span>&gt;(i,j) = (<span class="keywordtype">float</span>)<a class="code" href="../../d3/dc0/group__imgproc__shape.html#ga1a539e8db2135af2566103705d7a5722">pointPolygonTest</a>( contours[0], <a class="code" href="../../dc/d84/group__core__basic.html#ga7d080aa40de011e4410bca63385ffe2a">Point2f</a>((<span class="keywordtype">float</span>)j, (<span class="keywordtype">float</span>)i), <span class="keyword">true</span> );</div><div class="line">        }</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keywordtype">double</span> minVal, maxVal;</div><div class="line">    <a class="code" href="../../db/d4e/classcv_1_1Point__.html">Point</a> maxDistPt; <span class="comment">// inscribed circle center</span></div><div class="line">    <a class="code" href="../../d2/de8/group__core__array.html#gab473bf2eb6d14ff97e89b355dac20707">minMaxLoc</a>(raw_dist, &amp;minVal, &amp;maxVal, NULL, &amp;maxDistPt);</div><div class="line">    minVal = <a class="code" href="../../d2/d75/namespacecv.html#af6df65b17fb11af6d34634b6dfa44683">abs</a>(minVal);</div><div class="line">    maxVal = <a class="code" href="../../d2/d75/namespacecv.html#af6df65b17fb11af6d34634b6dfa44683">abs</a>(maxVal);</div><div class="line"></div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> drawing = <a class="code" href="../../d3/d63/classcv_1_1Mat.html#a0b57b6a326c8876d944d188a46e0f556">Mat::zeros</a>( src.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#a146f8e8dda07d1365a575ab83d9828d1">size</a>(), <a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga88c4cd9de76f678f33928ef1e3f96047">CV_8UC3</a> );</div><div class="line">    <span class="keywordflow">for</span>( <span class="keywordtype">int</span> i = 0; i &lt; src.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abed816466c45234254d25bc59c31245e">rows</a>; i++ )</div><div class="line">    {</div><div class="line">        <span class="keywordflow">for</span>( <span class="keywordtype">int</span> j = 0; j &lt; src.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa3e5a47585c9ef6a0842556739155e3e">cols</a>; j++ )</div><div class="line">        {</div><div class="line">            <span class="keywordflow">if</span>( raw_dist.at&lt;<span class="keywordtype">float</span>&gt;(i,j) &lt; 0 )</div><div class="line">            {</div><div class="line">                drawing.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa5d20fc86d41d59e4d71ae93daee9726">at</a>&lt;<a class="code" href="../../d6/dcf/classcv_1_1Vec.html">Vec3b</a>&gt;(i,j)[0] = (<a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga65f85814a8290f9797005d3b28e7e5fc">uchar</a>)(255 - <a class="code" href="../../d2/d75/namespacecv.html#af6df65b17fb11af6d34634b6dfa44683">abs</a>(raw_dist.at&lt;<span class="keywordtype">float</span>&gt;(i,j)) * 255 / minVal);</div><div class="line">            }</div><div class="line">            <span class="keywordflow">else</span> <span class="keywordflow">if</span>( raw_dist.at&lt;<span class="keywordtype">float</span>&gt;(i,j) &gt; 0 )</div><div class="line">            {</div><div class="line">                drawing.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa5d20fc86d41d59e4d71ae93daee9726">at</a>&lt;<a class="code" href="../../d6/dcf/classcv_1_1Vec.html">Vec3b</a>&gt;(i,j)[2] = (<a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga65f85814a8290f9797005d3b28e7e5fc">uchar</a>)(255 - raw_dist.at&lt;<span class="keywordtype">float</span>&gt;(i,j) * 255 / maxVal);</div><div class="line">            }</div><div class="line">            <span class="keywordflow">else</span></div><div class="line">            {</div><div class="line">                drawing.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa5d20fc86d41d59e4d71ae93daee9726">at</a>&lt;<a class="code" href="../../d6/dcf/classcv_1_1Vec.html">Vec3b</a>&gt;(i,j)[0] = 255;</div><div class="line">                drawing.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa5d20fc86d41d59e4d71ae93daee9726">at</a>&lt;<a class="code" href="../../d6/dcf/classcv_1_1Vec.html">Vec3b</a>&gt;(i,j)[1] = 255;</div><div class="line">                drawing.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa5d20fc86d41d59e4d71ae93daee9726">at</a>&lt;<a class="code" href="../../d6/dcf/classcv_1_1Vec.html">Vec3b</a>&gt;(i,j)[2] = 255;</div><div class="line">            }</div><div class="line">        }</div><div class="line">    }</div><div class="line">    <a class="code" href="../../d6/d6e/group__imgproc__draw.html#gaf10604b069374903dbd0f0488cb43670">circle</a>(drawing, maxDistPt, (<span class="keywordtype">int</span>)maxVal, <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(255,255,255));</div><div class="line"></div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga453d42fe4cb60e5723281a89973ee563">imshow</a>( <span class="stringliteral">&quot;Source&quot;</span>, src );</div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga453d42fe4cb60e5723281a89973ee563">imshow</a>( <span class="stringliteral">&quot;Distance and inscribed circle&quot;</span>, drawing );</div><div class="line"></div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">waitKey</a>();</div><div class="line">    <span class="keywordflow">return</span> 0;</div><div class="line">}</div></div><!-- fragment -->  </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'><p> This tutorial code's is shown lines below. You can also download it from <a href="https://github.com/opencv/opencv/tree/master/samples/java/tutorial_code/ShapeDescriptors/point_polygon_test/PointPolygonTestDemo.java">here</a> </p><div class="fragment"><div class="line"><span class="keyword">import</span> java.util.ArrayList;</div><div class="line"><span class="keyword">import</span> java.util.List;</div><div class="line"></div><div class="line"><span class="keyword">import</span> org.opencv.core.Core;</div><div class="line"><span class="keyword">import</span> org.opencv.core.Core.MinMaxLocResult;</div><div class="line"><span class="keyword">import</span> org.opencv.core.CvType;</div><div class="line"><span class="keyword">import</span> org.opencv.core.Mat;</div><div class="line"><span class="keyword">import</span> org.opencv.core.MatOfPoint;</div><div class="line"><span class="keyword">import</span> org.opencv.core.MatOfPoint2f;</div><div class="line"><span class="keyword">import</span> org.opencv.core.Point;</div><div class="line"><span class="keyword">import</span> org.opencv.core.Scalar;</div><div class="line"><span class="keyword">import</span> org.opencv.core.Size;</div><div class="line"><span class="keyword">import</span> org.opencv.highgui.HighGui;</div><div class="line"><span class="keyword">import</span> org.opencv.imgproc.Imgproc;</div><div class="line"></div><div class="line"><span class="keyword">class </span>PointPolygonTest {</div><div class="line">    <span class="keyword">public</span> <span class="keywordtype">void</span> run() {</div><div class="line">        <span class="keywordtype">int</span> r = 100;</div><div class="line">        Mat src = Mat.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#a0b57b6a326c8876d944d188a46e0f556">zeros</a>(<span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga346f563897249351a34549137c8532a0">Size</a>(4 * r, 4 * r), CvType.CV_8U);</div><div class="line"></div><div class="line">        List&lt;Point&gt; vert = <span class="keyword">new</span> ArrayList&lt;&gt;(6);</div><div class="line">        vert.add(<span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a>(3 * r / 2, 1.34 * r));</div><div class="line">        vert.add(<span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a>(1 * r, 2 * r));</div><div class="line">        vert.add(<span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a>(3 * r / 2, 2.866 * r));</div><div class="line">        vert.add(<span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a>(5 * r / 2, 2.866 * r));</div><div class="line">        vert.add(<span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a>(3 * r, 2 * r));</div><div class="line">        vert.add(<span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a>(5 * r / 2, 1.34 * r));</div><div class="line"></div><div class="line">        <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; 6; i++) {</div><div class="line">            Imgproc.line(src, vert.get(i), vert.get((i + 1) % 6), <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(255), 3);</div><div class="line">        }</div><div class="line"></div><div class="line">        List&lt;MatOfPoint&gt; contours = <span class="keyword">new</span> ArrayList&lt;&gt;();</div><div class="line">        Mat hierarchy = <span class="keyword">new</span> Mat();</div><div class="line">        Imgproc.findContours(src, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE);</div><div class="line"></div><div class="line">        Mat rawDist = <span class="keyword">new</span> Mat(src.size(), CvType.CV_32F);</div><div class="line">        <span class="keywordtype">float</span>[] rawDistData = <span class="keyword">new</span> <span class="keywordtype">float</span>[(int) (rawDist.total() * rawDist.channels())];</div><div class="line">        <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; src.rows(); i++) {</div><div class="line">            <span class="keywordflow">for</span> (<span class="keywordtype">int</span> j = 0; j &lt; src.cols(); j++) {</div><div class="line">                rawDistData[i * src.cols() + j] = (float) Imgproc</div><div class="line">                        .<a class="code" href="../../d3/dc0/group__imgproc__shape.html#ga1a539e8db2135af2566103705d7a5722">pointPolygonTest</a>(<span class="keyword">new</span> MatOfPoint2f(contours.get(0).toArray()), <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a>(j, i), <span class="keyword">true</span>);</div><div class="line">            }</div><div class="line">        }</div><div class="line">        rawDist.put(0, 0, rawDistData);</div><div class="line"></div><div class="line">        MinMaxLocResult res = Core.minMaxLoc(rawDist);</div><div class="line">        <span class="keywordtype">double</span> minVal = Math.abs(res.minVal);</div><div class="line">        <span class="keywordtype">double</span> maxVal = Math.abs(res.maxVal);</div><div class="line"></div><div class="line">        Mat drawing = Mat.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#a0b57b6a326c8876d944d188a46e0f556">zeros</a>(src.size(), CvType.CV_8UC3);</div><div class="line">        byte[] drawingData = <span class="keyword">new</span> byte[(int) (drawing.total() * drawing.channels())];</div><div class="line">        <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; src.rows(); i++) {</div><div class="line">            <span class="keywordflow">for</span> (<span class="keywordtype">int</span> j = 0; j &lt; src.cols(); j++) {</div><div class="line">                <span class="keywordflow">if</span> (rawDistData[i * src.cols() + j] &lt; 0) {</div><div class="line">                    drawingData[(i * src.cols() + j) * 3] =</div><div class="line">                            (byte) (255 - Math.abs(rawDistData[i * src.cols() + j]) * 255 / minVal);</div><div class="line">                } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (rawDistData[i * src.cols() + j] &gt; 0) {</div><div class="line">                    drawingData[(i * src.cols() + j) * 3 + 2] =</div><div class="line">                            (byte) (255 - rawDistData[i * src.cols() + j] * 255 / maxVal);</div><div class="line">                } <span class="keywordflow">else</span> {</div><div class="line">                    drawingData[(i * src.cols() + j) * 3] = (byte) 255;</div><div class="line">                    drawingData[(i * src.cols() + j) * 3 + 1] = (byte) 255;</div><div class="line">                    drawingData[(i * src.cols() + j) * 3 + 2] = (byte) 255;</div><div class="line">                }</div><div class="line">            }</div><div class="line">        }</div><div class="line">        drawing.put(0, 0, drawingData);</div><div class="line">        Imgproc.circle(drawing, res.maxLoc, (<span class="keywordtype">int</span>)res.maxVal, <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(255, 255, 255), 2, 8, 0);</div><div class="line"></div><div class="line">        HighGui.imshow(<span class="stringliteral">&quot;Source&quot;</span>, src);</div><div class="line">        HighGui.imshow(<span class="stringliteral">&quot;Distance and inscribed circle&quot;</span>, drawing);</div><div class="line"></div><div class="line">        HighGui.waitKey();</div><div class="line">        System.exit(0);</div><div class="line">    }</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">public</span> <span class="keyword">class </span>PointPolygonTestDemo {</div><div class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keywordtype">void</span> main(<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>[] args) {</div><div class="line">        <span class="comment">// Load the native OpenCV library</span></div><div class="line">        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);</div><div class="line"></div><div class="line">        <span class="keyword">new</span> PointPolygonTest().run();</div><div class="line">    }</div><div class="line"></div><div class="line">}</div></div><!-- fragment -->  </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'><p> This tutorial code's is shown lines below. You can also download it from <a href="https://github.com/opencv/opencv/tree/master/samples/python/tutorial_code/ShapeDescriptors/point_polygon_test/pointPolygonTest_demo.py">here</a> </p><div class="fragment"><div class="line"><span class="keyword">from</span> __future__ <span class="keyword">import</span> print_function</div><div class="line"><span class="keyword">from</span> __future__ <span class="keyword">import</span> division</div><div class="line"><span class="keyword">import</span> cv2 <span class="keyword">as</span> cv</div><div class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</div><div class="line"></div><div class="line"><span class="comment"># Create an image</span></div><div class="line">r = 100</div><div class="line">src = np.zeros((4*r, 4*r), dtype=np.uint8)</div><div class="line"></div><div class="line"><span class="comment"># Create a sequence of points to make a contour</span></div><div class="line">vert = [<span class="keywordtype">None</span>]*6</div><div class="line">vert[0] = (3*r//2, int(1.34*r))</div><div class="line">vert[1] = (1*r, 2*r)</div><div class="line">vert[2] = (3*r//2, int(2.866*r))</div><div class="line">vert[3] = (5*r//2, int(2.866*r))</div><div class="line">vert[4] = (3*r, 2*r)</div><div class="line">vert[5] = (5*r//2, int(1.34*r))</div><div class="line"></div><div class="line"><span class="comment"># Draw it in src</span></div><div class="line"><span class="keywordflow">for</span> i <span class="keywordflow">in</span> range(6):</div><div class="line">    <a class="code" href="../../d6/d6e/group__imgproc__draw.html#ga7078a9fae8c7e7d13d24dac2520ae4a2">cv.line</a>(src, vert[i],  vert[(i+1)%6], ( 255 ), 3)</div><div class="line"></div><div class="line"><span class="comment"># Get the contours</span></div><div class="line">contours, _ = <a class="code" href="../../d3/dc0/group__imgproc__shape.html#gae4156f04053c44f886e387cff0ef6e08">cv.findContours</a>(src, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)</div><div class="line"></div><div class="line"><span class="comment"># Calculate the distances to the contour</span></div><div class="line">raw_dist = np.empty(src.shape, dtype=np.float32)</div><div class="line"><span class="keywordflow">for</span> i <span class="keywordflow">in</span> range(src.shape[0]):</div><div class="line">    <span class="keywordflow">for</span> j <span class="keywordflow">in</span> range(src.shape[1]):</div><div class="line">        raw_dist[i,j] = <a class="code" href="../../d3/dc0/group__imgproc__shape.html#ga1a539e8db2135af2566103705d7a5722">cv.pointPolygonTest</a>(contours[0], (j,i), <span class="keyword">True</span>)</div><div class="line"></div><div class="line">minVal, maxVal, _, maxDistPt = <a class="code" href="../../d2/de8/group__core__array.html#ga8873b86a29c5af51cafdcee82f8150a7">cv.minMaxLoc</a>(raw_dist)</div><div class="line">minVal = <a class="code" href="../../d1/d10/classcv_1_1MatExpr.html#a30843fc6c148a00f5d300a7f43f3fbdc">abs</a>(minVal)</div><div class="line">maxVal = <a class="code" href="../../d1/d10/classcv_1_1MatExpr.html#a30843fc6c148a00f5d300a7f43f3fbdc">abs</a>(maxVal)</div><div class="line"></div><div class="line"><span class="comment"># Depicting the  distances graphically</span></div><div class="line">drawing = np.zeros((src.shape[0], src.shape[1], 3), dtype=np.uint8)</div><div class="line"><span class="keywordflow">for</span> i <span class="keywordflow">in</span> range(src.shape[0]):</div><div class="line">    <span class="keywordflow">for</span> j <span class="keywordflow">in</span> range(src.shape[1]):</div><div class="line">        <span class="keywordflow">if</span> raw_dist[i,j] &lt; 0:</div><div class="line">            drawing[i,j,0] = 255 - <a class="code" href="../../d1/d10/classcv_1_1MatExpr.html#a30843fc6c148a00f5d300a7f43f3fbdc">abs</a>(raw_dist[i,j]) * 255 / minVal</div><div class="line">        <span class="keywordflow">elif</span> raw_dist[i,j] &gt; 0:</div><div class="line">            drawing[i,j,2] = 255 - raw_dist[i,j] * 255 / maxVal</div><div class="line">        <span class="keywordflow">else</span>:</div><div class="line">            drawing[i,j,0] = 255</div><div class="line">            drawing[i,j,1] = 255</div><div class="line">            drawing[i,j,2] = 255</div><div class="line"></div><div class="line"><a class="code" href="../../d6/d6e/group__imgproc__draw.html#gaf10604b069374903dbd0f0488cb43670">cv.circle</a>(drawing,maxDistPt, int(maxVal),(255,255,255), 1, cv.LINE_8, 0)</div><div class="line"><a class="code" href="../../df/d24/group__highgui__opengl.html#gaae7e90aa3415c68dba22a5ff2cefc25d">cv.imshow</a>(<span class="stringliteral">&#39;Source&#39;</span>, src)</div><div class="line"><a class="code" href="../../df/d24/group__highgui__opengl.html#gaae7e90aa3415c68dba22a5ff2cefc25d">cv.imshow</a>(<span class="stringliteral">&#39;Distance and inscribed circle&#39;</span>, drawing)</div><div class="line"><a class="code" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">cv.waitKey</a>()</div></div><!-- fragment -->  </div> <h2>Explanation </h2>
<h2>Result </h2>
<p>Here it is: </p><div class="image">
<img src="../../Point_Polygon_Test_Source_Image.png" alt="Point_Polygon_Test_Source_Image.png"/>
</div>
 <div class="image">
<img src="../../Point_Polygon_Test_Result.jpg" alt="Point_Polygon_Test_Result.jpg"/>
</div>
 </div></div><!-- contents -->
<!-- HTML footer for doxygen 1.8.6-->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Fri Apr 2 2021 11:36:36 for OpenCV by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="../../doxygen.png" alt="doxygen"/>
</a> 1.8.13
</small></address>
<script type="text/javascript">
//<![CDATA[
addTutorialsButtons();
//]]>
</script>
</body>
</html>
